﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sirocco.Data.Entity;
using SiroccoTest.Common;

namespace SiroccoTest.Data.Entity
{
    [TestClass]
    public class EmailTest : BaseTest
    {
        private TestContext TestContextInstance;
        private const string Col1 = "XX";
        private const string UpdateValue = "XXX";

        #region Text Context
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return TestContextInstance;
            }
            set
            {
                TestContextInstance = value;
            }
        }
        #endregion

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// Test insert email to database using nhibernate 
        /// </summary>
        [TestMethod]
        public void EmailTestInsert()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Email
                var email = new Email {Address = Col1 };
                var generatedId = sess.Save(email);
                
                // Find the Email
                var email1 = sess.Load<Email>(generatedId);
                Assert.IsNotNull(email1, "Insert - Email not saved");

                tx.Commit();
            }
        }

        /// <summary>
        /// Test update email to database using nhibernate 
        /// </summary>
        [TestMethod]
        public void EmailTestUpdate()
        {
            using(var sess = GetSession())   
            using(var tx = sess.BeginTransaction())
            {
                // Insert Email
                var email = new Email {Address = Col1};
                var generatedId = sess.Save(email);

                // Find the Email
                var emailIns = sess.Load<Email>(generatedId);
                emailIns.Address = UpdateValue;
                sess.Update(emailIns);

                var emailList = sess.CreateQuery("from Email as e where Address = ?").SetString(0, UpdateValue).List();
                Assert.IsTrue(emailList.Count > 0, "Update Test - No Email Found");

                tx.Commit();
            }
        }

        /// <summary>
        /// Test delete email to database using nhibernate 
        /// </summary>
        [TestMethod]
        public void EmailTestDelete()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Email
                var email = new Email {Address = Col1};
                var generatedId = sess.Save(email);

                // Delete the Email
                var emailDel = sess.Load<Email>(generatedId);
                sess.Delete(emailDel);

                var emailList = sess.CreateQuery("from Email as e where Address = ?").SetString(0, Col1).List();
                Assert.IsTrue(emailList.Count == 0, "Delete Test - Email cannot be deleted");

                tx.Commit();
            }
        }

        /// <summary>
        /// Test insert multiple email into the database
        /// </summary>
        [TestMethod]
        public void EmailTestInsertMultiple()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Country
                var email = new Email { Address = Col1 };
                sess.Save(email); // Get the generated id. 

                var email2 = new Email { Address = Col1 };
                sess.Save(email2); // Get the generated id. 

                var emailList = sess.CreateQuery("from Email as c where c.Address = ?").SetString(0, Col1).List();
                Assert.IsTrue(emailList.Count == 2, "Insert count is not 2");

                tx.Commit();
            }
        }
    }
}
